<?php
/**
	* 写cookie
	* 会对传入的cookie值进行序列化
	*
	* @param string $name cookie的名称
	* @param string or array or object $value cookie值, 默认值为空，即''
	* @param int $expire cookie过期时间,默认值为0
	* @param string $path 当前cookie的有效路径，默认值为"/"
	* @param string $domain 当前cookie的有效域名, 默认值为空
	* @param boolean $secure 是否只充许以安全链接方式写cookie。 true:是；false(默认值):否
	* @param boolean $httpOnly 是否只充许通过http协议写cookie。 true:是；false(默认值):否
	* @return boolean true:成功；false:失败
	*/
class NcCookie extends CHttpCookie{
	public static function setCookie($name, $value='', $expire=0, $path='/', $domain='', $secure=false, $httpOnly=false)
	{
		$defaultResult = false;

		if ( !isset($name) || strlen($name)==0 )
		return $defaultResult;

		$cookie = Yii::app()->request->getCookies();
		$cookie[$name] = new CHttpCookie($name, serialize($value));
		$cookie[$name]->expire = time()+$expire;
		$cookie[$name]->path = $path;
		$cookie[$name]->domain = $domain;
		$cookie[$name]->secure = $secure;
		$cookie[$name]->httpOnly = $httpOnly;

		return true;
	}


	/**
	* 销毁cookie
	*
	* @param string $name cookie的名称
	* @return boolean 销毁成功,一直返回true
	*/
	public static function delCookie($name)
	{
		$cookie = Yii::app()->request->getCookies();
		if ( $cookie==null ) {
			return true;
		}

		unset($cookie[$name]);
		return true;
	}

	/**
	* 读cookie
	*
	* @param string $name cookie的名称
	* @return string cookie值
	*/
	public static function getCookie($name)
	{
		$cookie = Yii::app()->request->getCookies();
		if ( $cookie==null )
			return null;

		return unserialize($cookie[$name]->value);
	} 
}